<?php
namespace app\admin\controller;
use think\Controller;

class Article extends Controller
{
	private $obj;
	public function _initialize(){
		$this->obj = model("Article");
	}
    public function index()
    {
    	$articles = $this->obj->getFirstArticle();
 		return $this->fetch('',[
 			'articles' => $articles,
 		]);
    }
    
    public function add()
    {
 		return $this->fetch();
    }
    
    public function edit($id){
    	if(intval($id) < 1){
    		$this->error('参数不合法！');
    	}
    	
    	$article = $this->obj->get($id);
    	$articles = $this->obj->getFirstArticle();
 		return $this->fetch('',[
 			'articles' => $articles,
 			'article' => $article,
 		]);
    }
    
    public function save()
    {
 		//print_r($_POST);
 		//print_r(input('post.'));
 		//print_r(request()->post());
 		$data = input('post.');
 		$validate = validate('Article');
 		if(!$validate->scene('add')->check($data)){
 			$this->error($validate->getError());
 		}
 		//把$data 提交model层
 		$res = model('Article')->add($data);
 		if($res){
 			$this->success('新增成功');
 		}else{
 			$this->error('新增失败');
 		}
    }
}